﻿using Infragistics.Win.UltraMessageBox;
using Microsoft.VisualBasic.Devices;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using OrBitDataModule.Common;
using OrBitDataModule.Models;
using OrBitDataModule.UI.Base;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Sockets;
using System.Reflection.PortableExecutable;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace OrBitDataModule.UI.Setting
{
    public partial class SocketClientSetting : SettingForm
    {
        public SocketClientSetting()
        {
            InitializeComponent();
            this.Load += SocketClientSetting_Load;
            tb_port.KeyPress += Tb_port_KeyPress;
        }

        private void Tb_port_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != '\b')
            {
                if ((e.KeyChar < '0') || (e.KeyChar > '9'))
                {
                    e.Handled = true;
                }
            }
        }

        private void SocketClientSetting_Load(object sender, EventArgs e)
        {
            //暂时只考虑支持TCP
            this.cb_socketProtocol.Items.AddRange(Enum.GetNames(typeof(ProtocolType)).Where(t => t.Equals("TCP", StringComparison.OrdinalIgnoreCase)).ToArray());
            this.cb_networkInterfaces.Items.AddRange(NetworkUtils.GetAllNetworkAdapters().Select(i => i.Name).ToArray());
            this.cb_defaultEncoding.Items.AddRange(StringUtils.GetAllEncodingNames().ToArray());
        }

        public override string SettingName => "Socket客户端";
        public override string SettingType => "Socket Client";
        public override string GetSetting()
        {
            if (string.IsNullOrWhiteSpace(tb_machineNo.Text)) throw new Exception("设备编号不能为空");

            if (string.IsNullOrWhiteSpace(tb_ip.Text)) throw new Exception("IP地址不能为空");

            if (string.IsNullOrWhiteSpace(tb_port.Text)) throw new Exception("端口号不能为空");

            if (!Regex.IsMatch(tb_ip.Text, @"^((25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d)))\.){3}(25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d)))$")) throw new Exception("IP地址格式错误");

            if (!int.TryParse(tb_port.Text, out int port)) throw new Exception("端口号必须为数字");

            if (port < 0 || port > 65535) throw new Exception("端口号必须1到65535之间");

            //暂不限定网卡，默认空即为使用默认网卡
            //if (string.IsNullOrWhiteSpace(this.cb_networkInterfaces.Text)) throw new Exception("network interface can not be empty");
            if (string.IsNullOrWhiteSpace(this.cb_defaultEncoding.Text)) throw new Exception("默认编码不能为空");
            if (string.IsNullOrWhiteSpace(this.cb_socketProtocol.Text)) throw new Exception("传输协议不能为空");

            JObject jobj = new JObject
            {
                { "Networkinterface", this.cb_networkInterfaces.Text },
                { "ProtocolType", this.cb_socketProtocol.Text },
                { "DefaultEncoding", cb_defaultEncoding.Text },
                { "MachineNo", tb_machineNo.Text },
                { "IP", tb_ip.Text },
                { "Port", port }
            };
            return JsonConvert.SerializeObject(jobj);
        }
        public override void LoadSetting(string json)
        {
            SocketClient socketClient = JsonConvert.DeserializeObject<SocketClient>(json);
            if (socketClient != null)
            {
                //不在本机的网卡列表无法显示
                this.cb_networkInterfaces.Text = socketClient.Networkinterface;
                this.cb_socketProtocol.Text = socketClient.ProtocolType;
                this.cb_defaultEncoding.Text = socketClient.DefaultEncoding;
                this.tb_machineNo.Text = socketClient.MachineNo;
                this.tb_ip.Text = socketClient.IP;
                this.tb_port.Text = socketClient.Port.ToString();
            }
        }
    }
}
